<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : !disabled ? '修改' : '查看'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-col :span="24">
        <el-form-item prop="headImgUrl">
          <img :src="dataForm.headImgUrl" style="height: 60px;width: 60px;"/>
        </el-form-item>
      </el-col>

      <el-col :span="12">
        <el-form-item label="微信昵称" prop="nickname">
          <el-input v-model="dataForm.nickname" disabled="disabled" placeholder="用户名"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="性别" prop="gender">
          <el-select v-model="dataForm.gender" :disabled="disabled"  placeholder="用户的性别">
            <el-option
              v-for="item in genderList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="注册时间" prop="registerTime">
          <el-input v-model="dataForm.registerTime" disabled="disabled" placeholder="注册时间"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="最后登录时间" prop="lastLoginTime">
          <el-input v-model="dataForm.lastLoginTime" disabled="disabled" placeholder="最后登录时间"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="注册时间" prop="registerTime">
          <el-input v-model="dataForm.registerTime" disabled="disabled" placeholder="手机号"></el-input>
        </el-form-item>
      </el-col>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button v-if="!disabled" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        disabled: false,
        visible: false,
        dataForm: {
          id: '',
          userName: '',
          gender: '',
          certNo: '',
          lastLoginTime: '',
          realname: '',
          mobile: '',
          headImgUrl: '',
          registerTime: '',
          mallUserId: ''
        },
        dataRule: {
        },
        genderList: [{'label': '未知的性别', 'value': 0},
          {'label': '男性', 'value': 1},
          {'label': '女性', 'value': 2},
          {'label': '未说明的性别', 'value': 9}]
      }
    },
    methods: {
      init (id, disabled) {
        this.disabled = disabled
        this.dataForm.id = id || ''
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: `/mall/user/info/${this.dataForm.id}`,
              method: 'get'
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.user
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm']
          .validate((valid) => {
            if (valid) {
              this.$http({
                url: `/mall/user/${!this.dataForm.id ? 'save' : 'update'}`,
                method: 'post',
                data: this.dataForm
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500
                  })
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            }
          })
      }
    }
  }
</script>
